package com.action.pc.product;

import com.bean.Product;
import com.bean.Product_special_rel;
import com.bean.Sys_user;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.service.product.ProductService;
import com.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvjianqing on 2017/10/21.
 * 商品模块
 */
@Controller
@RequestMapping("product")
public class ProductAction extends BaseAction {
    @Autowired
    private ProductService productService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap, ProductVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("proname", vo.getProname());
        paraMap.put("online", vo.getOnline());
        paraMap.put("protype", vo.getProtype());
        Sys_user user = getSysUser();
        paraMap.put("cityId", user.getCityid());
        paraMap.put("merchantId", user.getMerchantid());
        Page page = productService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/product_list", modelMap);
    }

    @RequestMapping("modify")
    public ModelAndView modify(HttpServletRequest request, ModelMap modelMap, ProductVo vo) {
        Integer id = vo.getId();
        Product product = new Product();
        if (id != null) {
            product = productService.selectByPrimaryKey(id);
        }
        modelMap.put("product", product);
        return new ModelAndView("/pc/product_modify", modelMap);
    }

    @RequestMapping("active")
    @ResponseBody
    public String online(ModelAndJson modelAndJson, ProductVo vo) {
        Integer id = vo.getId();
        Byte online = vo.getOnline();
        if (id != null && online != null) {
            Product product = new Product();
            product.setId(id);
            product.setOnline(online);
            productService.updateByPrimaryKeySelective(product);
            modelAndJson.setTrue("操作成功");
        } else {
            modelAndJson.setFalse("-1", "操作失败");
        }
        return modelAndJson.toString();
    }

    @RequestMapping("save")
    public ModelAndView save(HttpServletRequest request, ModelMap modelMap, ProductVo vo, @RequestParam MultipartFile file) {
        String img = null;
        String smallImg = null;
        try {
            smallImg = UploadUtil.uploadSmallImg(file, request);
            img = UploadUtil.uploadImg(file, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Sys_user user = getSysUser();
        Product product = new Product();
        product.setId(vo.getId());
        product.setCityid(user.getCityid());
        product.setMerchantid(user.getMerchantid());
        product.setProname(vo.getProname());
        product.setShortname(vo.getShortname());
        product.setCoverimg(img);
        product.setSmimg(smallImg);
        product.setOldprice(new BigDecimal(vo.getOldprice()));
        product.setPrice(new BigDecimal(vo.getPrice()));
        int res = productService.saveProduct(product);
        if (res > 0) {
            return new ModelAndView("redirect:/product/list.htm", modelMap);
        } else {
            modelMap.put("product", product);
            return new ModelAndView("/pc/product_modify", modelMap);
        }
    }

    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, ModelMap modelMap, ProductVo vo) {
        Integer id = vo.getId();
        Product product = new Product();
        if (id != null) {
            product = productService.selectByPrimaryKey(id);
        }
        modelMap.put("product", product);
        return new ModelAndView("/pc/product_detail", modelMap);
    }

    @RequestMapping("toSpecial")
    @ResponseBody
    public String toSpecial(ModelAndJson modelAndJson, ProductVo vo) {
        Integer id = vo.getId();
        if (id != null) {
            List<Product_special_rel> list = productService.findByProductId(id);
            if (list != null && list.size() > 0) {
                for (Product_special_rel p : list) {
                    Byte type = p.getType();
                    if (type == 1) {//引流
                        modelAndJson.put("type1", true);
                    } else if (type == 2) {//推荐
                        modelAndJson.put("type2", true);
                    }
                }
            }
            modelAndJson.setTrue("操作成功");
        } else {
            modelAndJson.setFalse("-1", "操作失败");
        }
        return modelAndJson.toString();
    }

    @RequestMapping("saveSpecial")
    @ResponseBody
    public String saveSpecial(ModelAndJson modelAndJson, ProductVo vo) {
        Integer id = vo.getId();
        Boolean type1 = vo.getType1();
        Boolean type2 = vo.getType2();
        if (id != null) {
            List<Product_special_rel> list = new ArrayList<>();
            if (type1) {
                Product_special_rel p1 = new Product_special_rel();
                p1.setProid(id);
                p1.setType((byte) 1);
                p1.setSort(0);
                list.add(p1);
            }
            if (type2) {
                Product_special_rel p2 = new Product_special_rel();
                p2.setProid(id);
                p2.setType((byte) 2);
                p2.setSort(0);
                list.add(p2);
            }
            productService.updateByProductId(id, list);
            modelAndJson.setTrue("操作成功");
        } else {
            modelAndJson.setFalse("-1", "操作失败");
        }
        //Product_special_rel psr = new Product_special_rel();
        //psr.setProid(id);
        return modelAndJson.toString();
    }

    @RequestMapping("sortDra")
    public ModelAndView sortDra(HttpServletRequest request, ModelMap modelMap, ProductVo vo) {
        Sys_user user = getSysUser();
        List<Map> list = productService.getDraListByMerchantId(user.getMerchantid());
        modelMap.put("list", list);
        return new ModelAndView("/pc/product_sort_dra", modelMap);
    }

    @RequestMapping("saveSort")
    @ResponseBody
    public String saveSortDra(ModelAndJson modelAndJson, ProductVo vo) {
        Integer id = vo.getId();
        Integer sort = vo.getSort();
        if (id != null && sort!=null) {
            Product_special_rel p = new Product_special_rel();
            p.setId(id);
            p.setSort(sort);
            productService.updatePSRById(p);
            modelAndJson.setTrue("操作成功");
        } else {
            modelAndJson.setFalse("-1", "操作失败");
        }
        return modelAndJson.toString();
    }
    @RequestMapping("sortRecommend")
    public ModelAndView sortRecommend(HttpServletRequest request, ModelMap modelMap, ProductVo vo) {
        Sys_user user = getSysUser();
        List<Map> list = productService.getRecommendListByMerchantId(user.getMerchantid());
        modelMap.put("list", list);
        return new ModelAndView("/pc/product_sort_recommend", modelMap);
    }
}
